import math
import os
import re
import struct
import threading
from collections import defaultdict
from time import sleep

import pandas as pd
import numpy as np
import time
from commen.log import Logger
import openpyxl
import datetime
import commen.config
from tools.draw_pic import draw_multiple_data_picture


def percentage_error(data_error, total, sheet_obj):
    dict_percentage = {}
    for i in range(8, 100):
        key = sheet_obj.cell(1, i).value
        if key == "差异项":
            break
        if data_error.get(key):
            dict_percentage[key] = f"{key}的正确率：{(data_error.get(key) / total) * 100}%"
            print(f"{key}的正确率：{100 - round((data_error.get(key) / total) * 100, 2)}%")
        else:
            dict_percentage[key] = f"{key}的正确率：100%"
            print(f"{key}的正确率：100%")


def ImuDatacontinuity():
    # ---------读取config文件获取筛选字段内容--------------------------
    mpukey = commen.config.global_config.getRaw('keywords', 'suanfaimu')
    caninskey = commen.config.global_config.getRaw('keywords', 'cankeyimu')

    # ---------读取config文件获取筛选字段内容--------------------------
    logger = Logger(logger='TestMylog').getlog()
    # -----------------------创建excel--------------------------------------------
    # 获取当前时间
    time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
    # 获取当前路径
    result_root = os.getcwd()
    root = os.getcwd()
    result_path = result_root + r'\result' + "\\" + time_now
    log_path = root + r'\original-logfile' + '\\'
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    for root, dirs, files in os.walk(log_path):
        for file in files:
            if "mcu" in file:
                aa = os.path.join(root, file)
            if "can" in file:
                bb = os.path.join(root, file)

    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    book = openpyxl.Workbook()  # 打开一个将写的文件
    sheet = book.create_sheet(index=0)  # 在将写的文件创建sheet
    saveExcel = result_path + "IMU数据准确性验证.xlsx"
    sheet.cell(1, 2).value = "mpu上imu数据的系统时间"
    sheet.cell(1, 3).value = "can上imu数据系统时间"
    sheet.cell(1, 4).value = "mpu上imu数据内容"
    sheet.cell(1, 5).value = "can上imu数据内容"
    sheet.cell(1, 6).value = "对比数据信息"
    sheet.cell(1, 7).value = "数据是否一致"
    sheet.cell(1, 8).value = "x方向（前）加速度"
    sheet.cell(1, 9).value = "y方向（右）加速度"
    sheet.cell(1, 10).value = "z方向（下）加速度"
    sheet.cell(1, 11).value = "x方向（前）角速度"
    sheet.cell(1, 12).value = "y方向（右）角速度"
    sheet.cell(1, 13).value = "z方向（下）角速度"
    sheet.cell(1, 14).value = "imu温度"
    sheet.cell(1, 15).value = "IMU解算状态"
    sheet.cell(1, 16).value = "IMU状态"
    sheet.cell(1, 17).value = "差异项"

    row = 1
    # -----------------------创建excel--------------------------------------------

    canfile = open(bb, 'r', encoding='gbk')  # 打开can.log目标文件
    dataprocfile = open(aa, 'r', encoding='UTF-8')  # 打开dataproc.log文件
    logger.info("打开目标文件")
    canlines = canfile.readlines()
    mpulines = dataprocfile.readlines()
    logger.info("获取内容")

    mpusystemtimelist = []  # mpu数据的系统时间
    cansystemtimelist = []  # can数据的系统时间
    mpumessagelist = []  # mpu数据内容
    canmessagelist = []  # can数据内容
    mpu_utctimelist = []  # mpuutc时间
    can_utctimelist = []  # canutc时间
    logger.info("开始逐行读取")

    for lines in mpulines:  # 对内容 进行逐行读取

        if mpukey in lines:  # 通过关键字获取数据信息并存入到list中

            # mpusystemtime = lines[:lines.index("<thread")]
            # mpusystemtimelist.append(mpusystemtime.rstrip())
            # mpumessage = lines.split("+0:")
            # mpumessage1 = mpumessage[1].lstrip().replace(" ","").replace("-","").replace("+10:","").replace("+20:","").replace("+30:","").replace("+40:","").replace(")","")
            # mpumessagelist.append(mpumessage1.rstrip())
            # mpu_utctimelist.append(mpumessage1.rstrip()[5:16])

            mpusystemtime = lines[:lines.index("<thread")]
            mpusystemtimelist.append(mpusystemtime.rstrip())
            mpumessage = lines.split("+0:")
            mpumessage1 = mpumessage[1].lstrip().replace(" ", "").replace("-", "").replace("+10:", "").replace("+20:",
                                                                                                               "").replace(
                "+30:", "").replace(")", "")
            year = int(str(mpumessage1[4:6]), 16) + 1970
            month = int(str(mpumessage1[6:8]), 16)
            day = int(str(mpumessage1[8:10]), 16)
            hour = int(str(mpumessage1[10:12]), 16)
            minuet = int(str(mpumessage1[12:14]), 16)
            seconds = int(str(mpumessage1[14:16]), 16)
            millisecond1 = int(str(mpumessage1[16:18]), 16) * 256
            millisecond2 = int(str(mpumessage1[18:20]), 16)
            milliseconds = int(millisecond1) + int(millisecond2)
            # --------------------截取代表mcu数据时间的16进制内容并转换成时间-----------------------------
            if month < 10:
                month = "0" + str(month)
            if day < 10:
                day = "0" + str(day)
            if hour < 10:
                hour = "0" + str(hour)
            if minuet < 10:
                minuet = "0" + str(minuet)
            if seconds < 10:
                seconds = "0" + str(seconds)
            if milliseconds < 10:
                milliseconds = "00" + str(milliseconds)
            if 10 <= int(milliseconds) < 100:
                milliseconds = "0" + str(milliseconds)
            # ---------------------------判断时间是否是矫正前的1970年----------------------------------------
            if int(str(mpumessage1[4:6]), 16) == 0:
                mcutime = "1980" + "-" + "01" + "-" + "06" + " " + "00" + ":" + "00" + ":" + str(
                    seconds) + "." + str(
                    milliseconds)
                # sheet.cell(row, 2).value = mcutime



            # --------------------------------拼接矫正前的1970年时间并写入----------------------------------------
            # -----------------------------------------拼接矫正后的时间并写入----------------------------------------
            else:
                mcutime = str(year) + "-" + str(month) + "-" + str(day) + " " + str(hour) + ":" + str(
                    minuet) + ":" + str(
                    seconds) + "." + str(
                    milliseconds)

            mpumessagelist.append(mpumessage1.rstrip())
            mpu_utctimelist.append(mcutime)

            # utc时间：str(mpumessage1.rstrip()[5:7])+" "+str(mpumessage1.rstrip()[7:9])+" "+str(mpumessage1.rstrip()[9:11])+" "+str(mpumessage1.rstrip()[11:13])+" "+str(mpumessage1.rstrip()[13:15])
    for canline in canlines:  # 对内容 进行逐行读取
        if caninskey in canline:  # 通过关键字获取数据信息并存入到list中
            systemtime = canline[:canline.index("Rx")]
            cansystemtimelist.append(systemtime.rstrip())
            canmessage = canline.split("s-fd-brs 64")
            canmessagelist.append(canmessage[1].replace(" ", ""))
            receivetime_old = bin(int(str(canmessage[1].replace(" ", "")[46:57]), 16))  # 十进制字符串转换成二进制
            # -------------------------二进制字符串转换成整数并拼接成时间------------------------------------
            try:

                year = pd.to_datetime("1980-01-06") + pd.DateOffset(days=int(receivetime_old[0:16], 2))
                year = year.strftime('%Y-%m-%d')
                hour = str(int(receivetime_old[16:22], 2))
                minute = str(int(receivetime_old[22:28], 2))
                seconds = str(int(receivetime_old[28:34], 2))
                milliseconds = str(int(receivetime_old[34:], 2))
                if int(hour) < 10:
                    hour = "0" + str(hour)
                if int(minute) < 10:
                    minute = "0" + str(minute)
                if int(seconds) < 10:
                    seconds = "0" + str(seconds)
                if int(milliseconds) < 10:
                    milliseconds = "00" + str(milliseconds)
                if 10 <= int(milliseconds) < 100:
                    milliseconds = "0" + str(milliseconds)
                sf_receivetime = year + " " + hour + ":" + minute + ":" + seconds + "." + milliseconds
                can_utctimelist.append(sf_receivetime)
            except:
                continue
    pic_dict = defaultdict(int)
    total = 0
    for i in mpu_utctimelist:
        wash_data = {}
        if i in can_utctimelist:
            total += 1
            t1 = datetime.datetime.strptime(i, '%Y-%m-%d %H:%M:%S.%f')
            # pic_utctime.append(int(time.mktime(t1.timetuple()) * 1000.0 + t1.microsecond / 1000.0))
            logger.info("开始进行对比")
            row = row + 1
            mpu_number = mpu_utctimelist.index(str(i))
            can_number = can_utctimelist.index(str(i))
            try:
                # -----------------------------------------------IMU X-axis acceleration, x方向（前）加速度(4.19 check)---------------------------------------------
                mpu_xacceleration = mpumessagelist[mpu_number][20:28]
                # mpu_xacceleration = mpumessagelist[mpu_number][4:10]
                mpu_xacceleration1 = struct.unpack("I", (int(str(mpu_xacceleration), 16)).to_bytes(4, byteorder='big'))
                mpu_xacceleration2 = struct.unpack("i", (mpu_xacceleration1[0]).to_bytes(4, byteorder='big'))
                mpu_xacceleration3 = int(math.ceil(mpu_xacceleration2[0] / 915.527))
                mpu_xacceleration4 = mpu_xacceleration3 << 7
                if "-" in str(mpu_xacceleration4):
                    mpu_xacceleration_To_can1 = int(str(mpu_xacceleration4).replace("-", ""))
                    mpu_xacceleration_To_can = int(str(bin(~mpu_xacceleration_To_can1 + 1 & 0xfffffe)), 2)
                else:
                    mpu_xacceleration_To_can = mpu_xacceleration4

                # mpu_xacceleration_data1 = hex((mpu_xacceleration4>>16)&0xFF)[2:]
                # mpu_xacceleration_data2 = hex((mpu_xacceleration4 >> 8) & 0xFF)[2:]
                # mpu_xacceleration_data3 = hex(mpu_xacceleration4  & 0xFF)[2:]
                # if len(mpu_xacceleration_data1) < 2 and mpu_xacceleration_data1 == "0":
                #     mpu_xacceleration_data1 = str(mpu_xacceleration_data1) + "0"
                # if len(mpu_xacceleration_data1) < 2 and mpu_xacceleration_data1 != "0":
                #     mpu_xacceleration_data1 = "0" + str(mpu_xacceleration_data1)
                # if len(mpu_xacceleration_data2) < 2 and mpu_xacceleration_data2 == "0":
                #     mpu_xacceleration_data2 = str(mpu_xacceleration_data2) + "0"
                # if len(mpu_xacceleration_data2) < 2 and mpu_xacceleration_data2 != "0":
                #     mpu_xacceleration_data2 = "0" + str(mpu_xacceleration_data2)
                # if len(mpu_xacceleration_data3) < 2 and mpu_xacceleration_data3 == "0":
                #     mpu_xacceleration_data3 = str(mpu_xacceleration_data3) + "0"
                # if len(mpu_xacceleration_data3) < 2 and mpu_xacceleration_data3 != "0":
                #     mpu_xacceleration_data3 = "0" + str(mpu_xacceleration_data3)
                can_xacceleration = int(str(canmessagelist[can_number][4:10]), 16)
                wash_data[f"{mpu_xacceleration_To_can=}"] = f"{can_xacceleration=}"

                # -----------------------------------------------IMU Y-axis acceleration, y方向（右）加速度(4.19 check)---------------------------------------------
                mpu_yacceleration = mpumessagelist[mpu_number][28:36]
                # mpu_yacceleration = mpumessagelist[mpu_number][32:40]
                mpu_yacceleration1 = struct.unpack("I", (int(str(mpu_yacceleration), 16)).to_bytes(4, byteorder='big'))
                mpu_yacceleration2 = struct.unpack("i", (mpu_yacceleration1[0]).to_bytes(4, byteorder='big'))
                mpu_yacceleration3 = int(mpu_yacceleration2[0] / 915.527)
                mpu_yacceleration4 = mpu_yacceleration3 << 7
                if "-" in str(mpu_yacceleration4):
                    mpu_yacceleration_To_can1 = int(str(mpu_yacceleration4).replace("-", ""))
                    mpu_yacceleration_To_can = int(str(bin(~mpu_yacceleration_To_can1 + 1 & 0xfffffe)), 2)
                else:
                    mpu_yacceleration_To_can = mpu_yacceleration4
                # mpu_yacceleration_data1 = hex((mpu_yacceleration4 >> 16) & 0xFF)[2:]
                # mpu_yacceleration_data2 = hex((mpu_yacceleration4 >> 8) & 0xFF)[2:]
                # mpu_yacceleration_data3 = hex(mpu_yacceleration4 & 0xFF)[2:]
                # if len(mpu_yacceleration_data1) < 2 and mpu_yacceleration_data1 == "0":
                #     mpu_yacceleration_data1 = str(mpu_yacceleration_data1) + "0"
                # if len(mpu_yacceleration_data1) < 2 and mpu_yacceleration_data1 != "0":
                #     mpu_yacceleration_data1 = "0" + str(mpu_yacceleration_data1)
                # if len(mpu_yacceleration_data2) < 2 and mpu_yacceleration_data2 == "0":
                #     mpu_yacceleration_data2 = str(mpu_yacceleration_data2) + "0"
                # if len(mpu_yacceleration_data2) < 2 and mpu_yacceleration_data2 != "0":
                #     mpu_yacceleration_data2 = "0" + str(mpu_yacceleration_data2)
                # if len(mpu_yacceleration_data3) < 2 and mpu_yacceleration_data3 == "0":
                #     mpu_yacceleration_data3 = str(mpu_yacceleration_data3) + "0"
                # if len(mpu_yacceleration_data3) < 2 and mpu_yacceleration_data3 != "0":
                #     mpu_yacceleration_data3 = "0" + str(mpu_yacceleration_data3)
                # mpu_yacceleration_To_can = (mpu_yacceleration_data1 + mpu_yacceleration_data2 + mpu_yacceleration_data3).upper()
                can_yacceleration = int(str(canmessagelist[can_number][10:16]), 16)
                wash_data[f"{mpu_yacceleration_To_can=}"] = f"{can_yacceleration=}"
                # -----------------------------------------------IMU Z-axis acceleration, z方向（下）加速度(4.19 check)---------------------------------------------
                mpu_zacceleration = mpumessagelist[mpu_number][36:44]
                # mpu_zacceleration = mpumessagelist[mpu_number][16:22]
                mpu_zacceleration1 = struct.unpack("I", (int(str(mpu_zacceleration), 16)).to_bytes(4, byteorder='big'))
                mpu_zacceleration2 = struct.unpack("i", (mpu_zacceleration1[0]).to_bytes(4, byteorder='big'))
                mpu_zacceleration3 = int(mpu_zacceleration2[0] / 915.527)
                mpu_zacceleration4 = mpu_zacceleration3 << 7
                if "-" in str(mpu_zacceleration4):
                    mpu_zacceleration_To_can1 = int(str(mpu_zacceleration4).replace("-", ""))
                    mpu_zacceleration_To_can = int(str(bin(~mpu_zacceleration_To_can1 + 1 & 0xfffffe)), 2)
                else:
                    mpu_zacceleration_To_can = mpu_zacceleration4
                # mpu_zacceleration_data1 = hex((mpu_zacceleration4 >> 16) & 0xFF)[2:]
                # mpu_zacceleration_data2 = hex((mpu_zacceleration4 >> 8) & 0xFF)[2:]
                # mpu_zacceleration_data3 = hex(mpu_zacceleration4 & 0xFF)[2:]
                # if len(mpu_zacceleration_data1) < 2 and mpu_zacceleration_data1 == "0":
                #     mpu_zacceleration_data1 = str(mpu_zacceleration_data1) + "0"
                # if len(mpu_zacceleration_data1) < 2 and mpu_zacceleration_data1 != "0":
                #     mpu_zacceleration_data1 = "0" + str(mpu_zacceleration_data1)
                # if len(mpu_zacceleration_data2) < 2 and mpu_zacceleration_data2 == "0":
                #     mpu_zacceleration_data2 = str(mpu_zacceleration_data2) + "0"
                # if len(mpu_zacceleration_data2) < 2 and mpu_zacceleration_data2 != "0":
                #     mpu_zacceleration_data2 = "0" + str(mpu_zacceleration_data2)
                # if len(mpu_zacceleration_data3) < 2 and mpu_zacceleration_data3 == "0":
                #     mpu_zacceleration_data3 = str(mpu_zacceleration_data3) + "0"
                # if len(mpu_zacceleration_data3) < 2 and mpu_zacceleration_data3 != "0":
                #     mpu_zacceleration_data3 = "0" + str(mpu_zacceleration_data3)
                # mpu_zacceleration_To_can = (mpu_zacceleration_data1 + mpu_zacceleration_data2 + mpu_zacceleration_data3).upper()
                can_zacceleration = int(str(canmessagelist[can_number][16:22]), 16)
                wash_data[f"{mpu_zacceleration_To_can=}"] = f"{can_zacceleration=}"
                # -----------------------------------------------IMU X-axis angular rate, x方向（前）角速度---------------------------------------------
                mpu_xangular = mpumessagelist[mpu_number][44:52]
                mpu_xangular1 = struct.unpack("I", (int(str(mpu_xangular), 16)).to_bytes(4, byteorder='big'))
                mpu_xangular2 = struct.unpack("i", (mpu_xangular1[0]).to_bytes(4, byteorder='big'))
                mpu_xangular3 = int(mpu_xangular2[0] / 1144.4)
                mpu_xangular4 = mpu_xangular3 << 5
                if "-" in str(mpu_xangular4):
                    mpu_xangular_To_can1 = int(str(mpu_xangular4).replace("-", ""))
                    mpu_xangular_To_can = int(str(bin(~mpu_xangular_To_can1 + 1 & 0xfffff8)), 2)
                else:
                    mpu_xangular_To_can = mpu_xangular4

                # mpu_xangular_data1 = hex((mpu_xangular4 >> 16) & 0xFF)[2:]
                # mpu_xangular_data2 = hex((mpu_xangular4 >> 8) & 0xFF)[2:]
                # mpu_xangular_data3 = hex(mpu_xangular4 & 0xFF)[2:]
                # if len(mpu_xangular_data1) < 2 and mpu_xangular_data1 == "0":
                #     mpu_xangular_data1 = str(mpu_xangular_data1) + "0"
                # if len(mpu_xangular_data1) < 2 and mpu_xangular_data1 != "0":
                #     mpu_xangular_data1 = "0" + str(mpu_xangular_data1)
                # if len(mpu_xangular_data2) < 2 and mpu_xangular_data2 == "0":
                #     mpu_xangular_data2 = str(mpu_xangular_data2) + "0"
                # if len(mpu_xangular_data2) < 2 and mpu_xangular_data2 != "0":
                #     mpu_xangular_data2 = "0" + str(mpu_xangular_data2)
                # if len(mpu_xangular_data3) < 2 and mpu_xangular_data3 == "0":
                #     mpu_xangular_data3 = str(mpu_xangular_data3) + "0"
                # if len(mpu_xangular_data3) < 2 and mpu_xangular_data3 != "0":
                #     mpu_xangular_data3 = "0" + str(mpu_xangular_data3)
                # mpu_xangular_To_can = (mpu_xangular_data1 + mpu_xangular_data2 + mpu_xangular_data3).upper()
                can_xangular = int(str(canmessagelist[can_number][24:30]), 16)
                wash_data[f"{mpu_xangular_To_can=}"] = f"{can_xangular=}"
                # -----------------------------------------------IMU Y-axis angular rate, y方向（右）角速度---------------------------------------------
                mpu_yangular = mpumessagelist[mpu_number][52:60]
                mpu_yangular1 = struct.unpack("I", (int(str(mpu_yangular), 16)).to_bytes(4, byteorder='big'))
                mpu_yangular2 = struct.unpack("i", (mpu_yangular1[0]).to_bytes(4, byteorder='big'))
                mpu_yangular3 = int(mpu_yangular2[0] / 1144.4)
                mpu_yangular4 = mpu_yangular3 << 5
                if "-" in str(mpu_yangular4):
                    mpu_yangular_To_can1 = int(str(mpu_yangular4).replace("-", ""))
                    mpu_yangular_To_can = int(str(bin(~mpu_yangular_To_can1 + 1 & 0xfffff8)), 2)
                else:
                    mpu_yangular_To_can = mpu_yangular4
                # mpu_yangular_data1 = hex((mpu_yangular4 >> 16) & 0xFF)[2:]
                # mpu_yangular_data2 = hex((mpu_yangular4 >> 8) & 0xFF)[2:]
                # mpu_yangular_data3 = hex(mpu_yangular4 & 0xFF)[2:]
                # if len(mpu_yangular_data1) < 2 and mpu_yangular_data1 == "0":
                #     mpu_yangular_data1 = str(mpu_yangular_data1) + "0"
                # if len(mpu_yangular_data1) < 2 and mpu_yangular_data1 != "0":
                #     mpu_yangular_data1 = "0" + str(mpu_yangular_data1)
                # if len(mpu_yangular_data2) < 2 and mpu_yangular_data2 == "0":
                #     mpu_yangular_data2 = str(mpu_yangular_data2) + "0"
                # if len(mpu_yangular_data2) < 2 and mpu_yangular_data2 != "0":
                #     mpu_yangular_data2 = "0" + str(mpu_yangular_data2)
                # if len(mpu_yangular_data3) < 2 and mpu_yangular_data3 == "0":
                #     mpu_yangular_data3 = str(mpu_yangular_data3) + "0"
                # if len(mpu_yangular_data3) < 2 and mpu_yangular_data3 != "0":
                #     mpu_yangular_data3 = "0" + str(mpu_yangular_data3)
                # mpu_yangular_To_can = (
                #         mpu_yangular_data1 + mpu_yangular_data2 + mpu_yangular_data3).upper()
                can_yangular = int(str(canmessagelist[can_number][30:36]), 16)
                wash_data[f"{mpu_yangular_To_can=}"] = f"{can_yangular=}"
                # -----------------------------------------------IMU Z-axis angular rate, z方向（下）角速度---------------------------------------------
                mpu_zangular = mpumessagelist[mpu_number][60:68]
                mpu_zangular1 = struct.unpack("I", (int(str(mpu_zangular), 16)).to_bytes(4, byteorder='big'))
                mpu_zangular2 = struct.unpack("i", (mpu_zangular1[0]).to_bytes(4, byteorder='big'))
                mpu_zangular3 = int(mpu_zangular2[0] / 1144.4)
                mpu_zangular4 = mpu_zangular3 << 5
                if "-" in str(mpu_zangular4):
                    mpu_zangular_To_can1 = int(str(mpu_zangular4).replace("-", ""))
                    mpu_zangular_To_can = int(str(bin(~mpu_zangular_To_can1 + 1 & 0xfffff8)), 2)
                else:
                    mpu_zangular_To_can = mpu_zangular4
                # mpu_zangular_data1 = hex((mpu_zangular4 >> 16) & 0xFF)[2:]
                # mpu_zangular_data2 = hex((mpu_zangular4 >> 8) & 0xFF)[2:]
                # mpu_zangular_data3 = hex(mpu_zangular4 & 0xFF)[2:]
                # if len(mpu_zangular_data1) < 2 and mpu_zangular_data1 == "0":
                #     mpu_zangular_data1 = str(mpu_zangular_data1) + "0"
                # if len(mpu_zangular_data1) < 2 and mpu_zangular_data1 != "0":
                #     mpu_zangular_data1 = "0" + str(mpu_zangular_data1)
                # if len(mpu_zangular_data2) < 2 and mpu_zangular_data2 == "0":
                #     mpu_zangular_data2 = str(mpu_zangular_data2) + "0"
                # if len(mpu_zangular_data2) < 2 and mpu_zangular_data2 != "0":
                #     mpu_zangular_data2 = "0" + str(mpu_zangular_data2)
                # if len(mpu_zangular_data3) < 2 and mpu_zangular_data3 == "0":
                #     mpu_zangular_data3 = str(mpu_zangular_data3) + "0"
                # if len(mpu_zangular_data3) < 2 and mpu_zangular_data3 != "0":
                #     mpu_zangular_data3 = "0" + str(mpu_zangular_data3)
                # mpu_zangular_To_can = (
                #         mpu_zangular_data1 + mpu_zangular_data2 + mpu_zangular_data3).upper()
                can_zangular = int(str(canmessagelist[can_number][36:42]), 16)
                wash_data[f"{mpu_zangular_To_can=}"] = f"{can_zangular=}"
                # -----------------------------------------------imu温度---------------------------------------------
                mpu_temp = mpumessagelist[mpu_number][68:72]
                mpu_temp1 = int(str(mpu_temp), 16)
                mpu_temp2 = mpu_temp1 + 4000
                mpu_temp3 = int(mpu_temp2 / 9.2773)
                mpu_temp4 = mpu_temp3 << 5
                mpu_temp_To_can = int(str(hex(mpu_temp4)[2:]), 16)
                can_temp = int(str(canmessagelist[can_number][42:46]), 16)
                wash_data[f"{mpu_temp_To_can=}"] = f"{can_temp=}"
                # -----------------------------------------------IMU解算状态---------------------------------------------
                mpu_Cal_Status = mpumessagelist[mpu_number][72:74]
                mpu_Cal_Status1 = int(mpu_Cal_Status, 16) << 6
                mpu_Cal_Status_To_can = str(hex(int(mpu_Cal_Status1))[2:])
                can_Cal_Status = canmessagelist[can_number][22:24]
                wash_data[f"{mpu_Cal_Status_To_can=}"] = f"{can_Cal_Status=}"
                # -----------------------------------------------IMU状态---------------------------------------------
                mpu_Status = mpumessagelist[mpu_number][74:76]
                mpu_Status1 = int(mpu_Status, 16) << 6
                mpu_Status_To_can = str(hex(int(mpu_Status1))[2:])
                can_Status = canmessagelist[can_number][58:60]
                wash_data[f"{mpu_Status_To_can=}"] = f"{can_Status=}"
            # # -----------------------------------------------域控时间---------------------------------------------
            #             mpu_Timestamp_To_can = mpumessagelist[mpu_number][72:]
            #             can_Timestamp = canmessagelist[can_number][60:72]
            except:
                continue

            if mpu_xacceleration_To_can == can_xacceleration and mpu_yacceleration_To_can == can_yacceleration and mpu_zacceleration_To_can == can_zacceleration and mpu_xangular_To_can == can_xangular and mpu_yangular_To_can == can_yangular and mpu_zangular_To_can == can_zangular and mpu_temp_To_can == can_temp and mpu_Cal_Status_To_can == can_Cal_Status and mpu_Status_To_can == can_Status:
                sheet.cell(row, 2).value = mpusystemtimelist[mpu_number]
                sheet.cell(row, 3).value = cansystemtimelist[can_number]
                sheet.cell(row, 4).value = mpumessagelist[mpu_number]
                sheet.cell(row, 5).value = canmessagelist[can_number]
                sheet.cell(row, 6).value = "mpu_xacceleration:" + str(
                    mpu_xacceleration_To_can) + '\r\n' + "can_xacceleration:" + str(
                    can_xacceleration) + '\r\n' + "mpu_yacceleration_To_can:" + str(
                    mpu_yacceleration_To_can) + '\r\n' + "can_yacceleration:" + str(
                    can_yacceleration) + '\r\n' + "mpu_zacceleration_To_can:" + str(
                    mpu_zacceleration_To_can) + '\r\n' + "can_zacceleration:" + str(
                    can_zacceleration) + '\r\n' + "mpu_xangular_To_can:" + str(
                    mpu_xangular_To_can) + '\r\n' + "can_xangular:" + str(
                    can_xangular) + '\r\n' + "mpu_yangular_To_can:" + str(
                    mpu_yangular_To_can) + '\r\n' + "can_yangular:" + str(
                    can_yangular) + '\r\n' + "mpu_zangular_To_can:" + str(
                    mpu_zangular_To_can) + '\r\n' + "can_zangular:" + str(
                    can_zangular) + '\r\n' + "mpu_temp_To_can:" + str(mpu_temp_To_can) + '\r\n' + "can_temp:" + str(
                    can_temp) + '\r\n' + "mpu_Cal_Status_To_can:" + str(
                    mpu_Cal_Status_To_can) + '\r\n' + "can_Cal_Status:" + str(
                    can_Cal_Status) + '\r\n' + "mpu_Status_To_can:" + str(
                    mpu_Status_To_can) + '\r\n' + "can_Status:" + str(can_Status) + '\r\n' + "mpu_Timestamp_To_can:"
                sheet.cell(row, 7).value = "是"
            else:
                sheet.cell(row, 2).value = mpusystemtimelist[mpu_number]
                sheet.cell(row, 3).value = cansystemtimelist[can_number]
                sheet.cell(row, 4).value = mpumessagelist[mpu_number]
                sheet.cell(row, 5).value = canmessagelist[can_number]
                sheet.cell(row, 6).value = "mpu_xacceleration:" + str(
                    mpu_xacceleration_To_can) + '\r\n' + "can_xacceleration:" + str(
                    can_xacceleration) + '\r\n' + "mpu_yacceleration_To_can:" + str(
                    mpu_yacceleration_To_can) + '\r\n' + "can_yacceleration:" + str(
                    can_yacceleration) + '\r\n' + "mpu_zacceleration_To_can:" + str(
                    mpu_zacceleration_To_can) + '\r\n' + "can_zacceleration:" + str(
                    can_zacceleration) + '\r\n' + "mpu_xangular_To_can:" + str(
                    mpu_xangular_To_can) + '\r\n' + "can_xangular:" + str(
                    can_xangular) + '\r\n' + "mpu_yangular_To_can:" + str(
                    mpu_yangular_To_can) + '\r\n' + "can_yangular:" + str(
                    can_yangular) + '\r\n' + "mpu_zangular_To_can:" + str(
                    mpu_zangular_To_can) + '\r\n' + "can_zangular:" + str(
                    can_zangular) + '\r\n' + "mpu_temp_To_can:" + str(mpu_temp_To_can) + '\r\n' + "can_temp:" + str(
                    can_temp) + '\r\n' + "mpu_Cal_Status_To_can:" + str(
                    mpu_Cal_Status_To_can) + '\r\n' + "can_Cal_Status:" + str(
                    can_Cal_Status) + '\r\n' + "mpu_Status_To_can:" + str(
                    mpu_Status_To_can) + '\r\n' + "can_Status:" + str(can_Status) + '\r\n' + "mpu_Timestamp_To_can:"
                sheet.cell(row, 7).value = "否"
            diff_list = []
            loc = 8
            for k, v in wash_data.items():
                if k.split("=")[1] == v.split("=")[1]:
                    sheet.cell(row, loc).value = True
                else:
                    sheet.cell(row, loc).value = False
                    diff_list.append(k)
                    diff_list.append(v)
                    pic_dict[sheet.cell(1, loc).value] += 1
                loc += 1
            sheet.cell(row, loc).value = "\r\n".join(diff_list)
    sleep(2)
    print(pic_dict, total)
    percentage_error(pic_dict, total, sheet)
    # ----------------------将对比的数据写入excel---------------------------------
    book.save(saveExcel)
    # ----------------------将对比的数据写入excel---------------------------------


ImuDatacontinuity()
